package com.export.xlsx.dao.impl;

import com.export.xlsx.dao.CompanyTypeDao;
import com.export.xlsx.domain.CompanyType;
import com.export.xlsx.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ==================================================================
 * <p>
 * Description: 企业类型DaoImpl
 * <p>
 * Author: ryu
 * <p>
 * Date: 2018/1/24
 * <p>
 * Version: v1.0
 * <p>
 * Copyright: All rights reserved by Ryu.
 * ==================================================================
 */
public class CompanyTypeDaoImpl implements CompanyTypeDao {

    @Override
    public CompanyType queryByName(String name) {
        try {
            QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());

            String sql = "SELECT id, code, name FROM TB_COMPANY_TYPE t WHERE t.NAME=? ";
            Object[] params = {name};

            ResultSetHandler<CompanyType> rsh = new BeanHandler<CompanyType>(CompanyType.class) {

                @Override
                public CompanyType handle(ResultSet rs) throws SQLException {
                    CompanyType resultData = null;
                    while (rs.next()){
                        resultData = new CompanyType();
                        resultData.setId(rs.getInt("id"));
                        resultData.setCode(rs.getInt("code"));
                        resultData.setName(rs.getString("name"));
                    }
                    return resultData;
                }
            };
            return runner.query(sql, rsh, params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

    }

}
